#include "includes.h"




S_LOCAL_ITEM stLocalitem;

static MOMAP_S map_par_a[] =
{
	{"host_addr", 20, stLocalitem.host_addr, 0, 0, IS_ADDR_IP},
	{"host_mac", 20, stLocalitem.host_mac, 0, 0, IS_ADDR_MAC},
	{"guest_addr", 20, stLocalitem.guest_addr, 0, 0, IS_ADDR_IP},
	{"guest_mac", 20, stLocalitem.guest_mac, 0, 0, IS_ADDR_MAC},
	{"passwd", 20, stLocalitem.passwd, 0, 0, IS_STRING},
	{"cfg_name", 40, stLocalitem.cfg_name, 0, 0, IS_STRING},
};


int local_set_cfgname(char *name)
{
	strncpy(stLocalitem.cfg_name, name, 40);
	stLocalitem.cfg_name[39] = '\0';
	return 0;
}

int local_get_cfgname(char *name, int len)
{
	if (len < strlen(stLocalitem.cfg_name)+1)
		return -1;
	strcpy(name, stLocalitem.cfg_name);
	return 0;
}


int local_set_passwd(char *word)
{
	strncpy(stLocalitem.passwd, word, 20);
	stLocalitem.passwd[19] = '\0';
	return 0;
}

int local_get_passwd(char *word, int len)
{
	if (len < strlen(stLocalitem.passwd)+1)
		return -1;
	strcpy(word, stLocalitem.passwd);
	return 0;
}


int local_set_host_addr(char *addr)
{
	char cmd[100];	
	strncpy(stLocalitem.host_addr, addr, 20);
	stLocalitem.host_addr[19] = '\0';
	err_msg("cmd <%s>", cmd);
	snprintf(cmd, sizeof(cmd), "ifconfig eth0 %s", stLocalitem.host_addr);	
	system(cmd);
	return 0;
}

int local_set_host_mac(char *mac)
{
	char cmd[100];
	strncpy(stLocalitem.host_mac, mac, 20);
	stLocalitem.host_mac[19] = '\0';
	snprintf(cmd, sizeof(cmd), "ifconfig eth0 hw ether %s", stLocalitem.host_mac);
	system("ifconfig eth0 down");
	sleep(1);
	err_msg("cmd <%s>", cmd);
	system(cmd);
	system("ifconfig eth0 up");
	sleep(1);
	return 0;
}

int local_get_host_addr(char *addr, int len)
{
	if (len < 20)
		return -1;
	strncpy(addr, stLocalitem.host_addr, 20);
	return 0;
}

int local_get_host_mac(char *mac, int len)
{
	if (len < 20)
		return -1;
	strncpy(mac, stLocalitem.host_mac, 20);
	return 0;
}

int local_set_guest_addr(char *addr)
{
	char cmd[100];
	int ret;
	strncpy(stLocalitem.guest_addr, addr, 20);
	stLocalitem.guest_addr[19] = '\0';
	snprintf(cmd, sizeof(cmd), "ifconfig eth0 %s\n", stLocalitem.guest_addr);
	ret = uart_cmd(cmd, "#", 4000);
	return ret;
}

int local_set_guest_mac(char *mac)
{
	char cmd[100];
	int ret;
	strncpy(stLocalitem.guest_mac, mac, 20);
	stLocalitem.guest_mac[19] = '\0';
	snprintf(cmd, sizeof(cmd), "ifconfig eth0 hw ether %s\n", stLocalitem.guest_mac);
	uart_cmd_write("ifconfig eth0 down\n", strlen("ifconfig eth0 down\n"));
	sleep(1);
	uart_cmd(cmd, "#", 4000);
	ret = uart_cmd("ifconfig eth0 up\n", "wake up", 4000);	
	return ret;
}

int local_get_guest_addr(char *addr, int len)
{
	if (len < 20)
		return -1;
	strncpy(addr, stLocalitem.guest_addr, 20);
	return 0;
}

int local_get_guest_mac(char *mac, int len)
{
	if (len < 20)
		return -1;
	strncpy(mac, stLocalitem.guest_mac, 20);
	return 0;
}




/*
	<?xml version="1.0" encoding="UTF-8"?>
	<xml>
	<host_addr>192.168.13.122</host_addr>
	<host_mac>00:50:04:8b:0d:7a</host_mac>
	<guest_addr>192.168.13.123</guest_addr>
	<guest_mac>00:50:04:8b:0d:7b</guest_mac>
	<passwd>123456</passwd>
	<cfg_name>syscfg.xml</cfg_name>
	</xml>
*/
int load_local_item()
{
	FILE *fp;
	char cmd[100];
	int ret;
	mxml_node_t *tree;
	fp = Fopen(LOCAL_CONFIG, "r");
	tree = mxmlLoadFile(NULL, fp, MXML_TEXT_CALLBACK);
	if (tree == NULL)
	{
		err_ret("load the local configure failed");
		return -1;
	}

	ret = momap_decode_node(tree, tree, map_par_a, ARRAY_SIZE(map_par_a));
	mxmlDelete(tree);
	Fclose(fp);
	snprintf(cmd, sizeof(cmd), "ifconfig eth0 hw ether %s", stLocalitem.host_mac);
	system("ifconfig eth0 down");
	sleep(1);
	err_msg("cmd <%s>", cmd);
	system(cmd);
	system("ifconfig eth0 up");
	sleep(1);

	
	snprintf(cmd, sizeof(cmd), "ifconfig eth0 %s", stLocalitem.host_addr);
	err_msg("cmd <%s>", cmd);
	system(cmd);
	return ret;
}

static const char *whitespace_cb(mxml_node_t *node, int where)
{
	if (mxmlGetElement(node) && where==MXML_WS_AFTER_CLOSE)
		return ("\n");
	else
		return (NULL);
}



int save_local_item()
{
	return momap_encode_file(LOCAL_CONFIG, map_par_a, ARRAY_SIZE(map_par_a), whitespace_cb);
}

